//
//  KooCalendarCell.m
//  KooIEDUCalendarDemo
//
//  Created by nowcoder on 2024.
//

#import "KooCalendarCell.h"
#import "KooCalendarModel.h"
#import "KooCalendarConfig.h"
#import <Masonry/Masonry.h>

@interface KooCalendarCell ()

@property (nonatomic, strong) UILabel *dateLabel;


@end

@implementation KooCalendarCell

static NSString * const KooCalendarCellIdentifier = @"KooCalendarCell";

#pragma mark - Class Methods

+ (NSString *)cellIdentifier {
    return KooCalendarCellIdentifier;
}

#pragma mark - Initialization

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

#pragma mark - UI Setup

- (void)setupUI {
    self.contentView.backgroundColor = [UIColor clearColor];
    
    // 创建背景视图
    self.customBackgroundView = [[UIView alloc] init];
    self.customBackgroundView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.customBackgroundView];
    
    // 创建日期标签
    self.dateLabel = [[UILabel alloc] init];
    self.dateLabel.textAlignment = NSTextAlignmentCenter;
    self.dateLabel.backgroundColor = [UIColor clearColor];
    [self.customBackgroundView addSubview:self.dateLabel];
}

- (void)setupConstraints {
    // 背景视图约束
    [self.customBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.contentView);
        make.size.equalTo(self.contentView);
    }];
    
    // 日期标签约束
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.customBackgroundView);
        make.size.equalTo(self.customBackgroundView);
    }];
}

- (void)updateAppearance {
    if (!self.config || !self.cellModel) return;
    
    // 设置日期文本
    if (self.cellModel.displayText) {
        self.dateLabel.text = self.cellModel.displayText;
    } else {
        self.dateLabel.text = @(self.cellModel.day).stringValue;
    }
    
    // 根据状态设置样式
    switch (self.cellModel.state) {
        case KooCalendarCellStateToday:
            [self applyTodayStyle];
            break;
        case KooCalendarCellStateSelected:
            [self applySelectedStyle];
            break;
        case KooCalendarCellStateDisabled:
            [self applyDisabledStyle];
            break;
        case KooCalendarCellStateOtherMonth:
            [self applyOtherMonthStyle];
            break;
        case KooCalendarCellStateNormal:
        default:
            [self applyNormalStyle];
            break;
    }
    
    // 设置圆角
    if (self.config.enableCornerRadius) {
        self.customBackgroundView.layer.cornerRadius = self.config.cellCornerRadius;
        self.customBackgroundView.layer.masksToBounds = YES;
    } else {
        self.customBackgroundView.layer.cornerRadius = 0;
        self.customBackgroundView.layer.masksToBounds = NO;
    }
}

#pragma mark - Style Methods

- (void)clearBorderAndResetText {
    // 清除边框
    self.customBackgroundView.layer.borderWidth = 0;
    self.customBackgroundView.layer.borderColor = [UIColor clearColor].CGColor;
    
    // 重置文本内容
    self.dateLabel.numberOfLines = 1;
    if (self.cellModel.displayText) {
        self.dateLabel.text = self.cellModel.displayText;
    } else {
        self.dateLabel.text = @(self.cellModel.day).stringValue;
    }
}

- (void)applyTodayStyle {
    self.customBackgroundView.backgroundColor = self.config.todayBackgroundColor;
    self.dateLabel.textColor = self.config.todayTextColor;
    self.dateLabel.font = self.config.dateFont;
    self.contentView.alpha = 1.0;
    
    // 清除边框和重置文本
    [self clearBorderAndResetText];
}

- (void)applySelectedStyle {
    self.customBackgroundView.backgroundColor = self.config.selectedBackgroundColor;
    self.dateLabel.textColor = self.config.selectedTextColor;
    self.dateLabel.font = self.config.dateFont;
    self.contentView.alpha = 1.0;
    
    // 为图片设计主题添加绿色边框
    if ([self.config.selectedBackgroundColor isEqual:[UIColor clearColor]]) {
        self.customBackgroundView.layer.borderWidth = 2.0;
        self.customBackgroundView.layer.borderColor = self.config.selectedTextColor.CGColor;
        
        // 添加✓标记（通过修改文本内容）
        if (self.cellModel.displayText) {
            self.dateLabel.text = [NSString stringWithFormat:@"%@\n✓", self.cellModel.displayText];
        } else {
            self.dateLabel.text = [NSString stringWithFormat:@"%@\n✓", @(self.cellModel.day).stringValue];
        }
        self.dateLabel.numberOfLines = 2;
        self.dateLabel.font = [UIFont systemFontOfSize:self.config.dateFont.pointSize - 2];
    } else {
        // 清除边框（其他主题）
        [self clearBorderAndResetText];
    }
}

- (void)applyNormalStyle {
    self.customBackgroundView.backgroundColor = [UIColor clearColor];
    self.dateLabel.textColor = self.config.normalTextColor;
    self.dateLabel.font = self.config.dateFont;
    self.contentView.alpha = 1.0;
    
    // 清除边框和重置文本
    [self clearBorderAndResetText];
}

- (void)applyDisabledStyle {
    self.customBackgroundView.backgroundColor = [UIColor clearColor];
    self.dateLabel.textColor = self.config.disabledTextColor;
    self.dateLabel.font = self.config.dateFont;
    self.contentView.alpha = 0.5;
    
    // 清除边框和重置文本
    [self clearBorderAndResetText];
}

- (void)applyOtherMonthStyle {
    self.customBackgroundView.backgroundColor = [UIColor clearColor];
    self.dateLabel.textColor = self.config.disabledTextColor;
    self.dateLabel.font = self.config.dateFont;
    self.contentView.alpha = 0.3;
    
    // 清除边框和重置文本
    [self clearBorderAndResetText];
}

#pragma mark - Public Methods

- (void)configureCellWithModel:(KooCalendarCellModel *)cellModel config:(KooCalendarConfig *)config {
    self.cellModel = cellModel;
    self.config = config;
    [self updateAppearance];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if (!self.cellModel || !self.config) return;
    
    // 只有可选择的日期才能设置选中状态
    if (!self.cellModel.isSelectable) return;
    
    KooCalendarCellState newState = selected ? KooCalendarCellStateSelected : 
        (self.cellModel.isToday ? KooCalendarCellStateToday : KooCalendarCellStateNormal);
    
    if (animated) {
        [UIView animateWithDuration:self.config.selectAnimationDuration animations:^{
            self.cellModel.state = newState;
            [self updateAppearance];
        }];
    } else {
        self.cellModel.state = newState;
        [self updateAppearance];
    }
}

- (void)setTodayMark:(BOOL)isToday {
    if (!self.cellModel || !self.config) return;
    
    if (isToday && self.cellModel.state != KooCalendarCellStateSelected) {
        self.cellModel.state = KooCalendarCellStateToday;
        [self updateAppearance];
    }
}

- (void)setDisabled:(BOOL)disabled {
    if (!self.cellModel || !self.config) return;
    
    if (disabled) {
        self.cellModel.state = KooCalendarCellStateDisabled;
        [self updateAppearance];
    }
}

- (void)resetCellState {
    self.cellModel = nil;
    self.config = nil;
    
    // 重置UI状态
    self.customBackgroundView.backgroundColor = [UIColor clearColor];
    self.customBackgroundView.layer.cornerRadius = 0;
    self.dateLabel.text = @"";
    self.dateLabel.textColor = [UIColor blackColor];
    self.dateLabel.font = [UIFont systemFontOfSize:16];
    self.contentView.alpha = 1.0;
}

#pragma mark - UICollectionViewCell

- (void)prepareForReuse {
    [super prepareForReuse];
    [self resetCellState];
}

#pragma mark - Setters

- (void)setCellModel:(KooCalendarCellModel *)cellModel {
    _cellModel = cellModel;
    
    if (self.config && cellModel) {
        [self updateAppearance];
    }
}

- (void)setConfig:(KooCalendarConfig *)config {
    _config = config;
    
    if (self.cellModel && config) {
        [self updateAppearance];
    }
}

@end 